/**
 * Created by tuyican on 17/5/3.
 * 准备存放所有计划用到的公共方法
 */
function arrRepeat(onearr,towarr){
  //let arr = onearr.splice(0);
  //arr[0] = 0;
  //console.log(arr);
  for(let i=0;i<onearr.length;i++){
    for (let j=0;j<towarr.length;j++){
      if(onearr[i].userId===towarr[j].userId){
        onearr[i].state = true;
      }
    }
  }

  return onearr
}
//拼接出差人员
function travel_users(list){
  //let list = this.$store.state.traveldata.usurdata;
  let listarr = [];
  let name = ''
  for (let i=0;i<list.length;i++){
    let obj={
      userType:list[i].userType||'',
      userId:list[i].userId||'',
      fullname:list[i].fullname
    }
    listarr.push(obj);
  }
  return listarr
}
//拼接出差费用
function travel_money(money){
  //let list = this.$store.state.traveldata.usurdata;
  let moneyarr = [];
  for (let i=0;i<money.length;i++){
    let obj={
      expenseItemId:money[i].id,
      expenseItemName:money[i].name,
      amount:money[i].unm*100||0,
      budgetAccount:"",
    }
    moneyarr.push(obj);
  }
  return moneyarr
}
//全部的时间换算
let allDate = function(uData){
  if(!uData){
    return '';
  }
  if(typeof uData == 'string'){
    uData = uData.replace(/-/g, "/");
  }
  let myDate = new Date(uData);
  let year = myDate.getFullYear();
  let month = myDate.getMonth() + 1;
  let day = myDate.getDate();
  let hh = myDate.getHours();
  let mm = myDate.getMinutes();
  let ss = myDate.getSeconds();
  month = month<10?`0${month}`:month;
  day = day<10?`0${day}`:day;
  hh = hh<10?`0${hh}`:hh;
  mm = mm<10?`0${mm}`:mm;
  ss = ss<10?`0${ss}`:ss;
  return `${year}-${month}-${day}  ${hh}:${mm}:${ss}`;
}
//30分钟倒计时
function date_fresh(time,callback, total = 30){
  var itme = null;
  itme=setInterval(function(){
    var datetext = '';
    var d = time;
    var jias = total*60;
    var endtime = new Date(d);
    var nowtime = new Date();
    var leftsecond = parseInt((endtime.getTime() - nowtime.getTime()) / 1000+jias);
    //console.log(leftsecond)
    //d = parseInt(leftsecond / 3600 / 24);
    var h = parseInt((leftsecond / 3600) % 24);
    var m = parseInt((leftsecond / 60) % 60);
    var s = parseInt(leftsecond % 60);
    if(m<10){
      m = '0'+m;
    }
    if(s<10){
      s = '0'+s;
    }
    if(leftsecond<=0){
      clearInterval(itme);
      datetext = '0'
      callback&&callback();
      callback(datetext)
    }else{
      datetext = `${m}分${s}秒`;
      callback&&callback();
      callback(datetext)
    }
  },1000)
}
//神拷贝
function copyArr(arr){
  return arr.map((e)=>{
    if(typeof e === 'object'){
      return Object.assign({},e)
    }else{
      return e
    }
  })
}
//数组去重
function array_heavy(arr){
  var res = [];
  var json = {};
  for(var i = 0; i < arr.length; i++){
    if(!json[arr[i]]){
      res.push(arr[i]);
      json[arr[i]] = 1;
    }
  }
  return res;
}
//分割数组
function splcks(arr,index){
  var b = [];
  var result = [];
  var k = 0;
  for(var i = 0; i<arr.length; ++i){
    if(i%index == 0){
      b = [];
      for(var j = 0; j<index; ++j){
        if(arr[i+j] == undefined){
          continue;
        } else{
          b[j] = arr[i+j];
        }
      }
      result[k] = b;
      k++;
    }
  }
  return result;
}
export {arrRepeat,travel_users,travel_money,allDate,date_fresh,copyArr,array_heavy,splcks}
